<?php

namespace app\api\controller;


use app\model\member\Config as ConfigModel;
use app\model\member\MrnaApply;
use app\model\order\OrderCommon as OrderCommonModel;

class Mrna extends BaseApi
{

    //申请
    public function apply()
    {
        $token = $this->checkToken();
        if ($token[ 'code' ] < 0) return $this->response($token);

        $apply_model = new MrnaApply();

        $apply_data = [
            'member_id'=>$this->member_id,
            'name'=>input('name',''),
            'mobile'=>input('mobile',''),
            'front_pic_id_card'=>input('front_pic_id_card',''),//身份证正面图片
            'reverse_pic_id_card'=>input('reverse_pic_id_card',''),//身份证反面图片
            'id_card_name'=>input('id_card_name',''),//证件姓名
            'id_card_number'=>input('id_card_number',''),//证件号码
            'id_card_type'=>input('id_card_type',''),//证件类型
            'id_card_start_time'=>input('id_card_start_time',''),//证件有效期开始时间
            'id_card_end_time'=>input('id_card_end_time',''),//证件有效期结束时间
            'account_holder'=>input('account_holder',''),//开户人
            'bank_number'=>input('bank_number',''),//银行卡号
            'bank_type'=>input('bank_type',''),//银行卡类型
            'bank_name'=>input('bank_name',''),//开户行名称
            'bank_img'=>input('bank_img',''),
            'id_card_time_type'=>input('id_card_time_type',0)
        ];

        $res = $apply_model->apply($apply_data);

        return $this->response($res);
    }

    //编辑
    public function editMrna()
    {
        $token = $this->checkToken();
        if ($token[ 'code' ] < 0) return $this->response($token);

        $apply_info = model('mrna_apply')->getInfo([ [ 'member_id', '=', $this->member_id ] ]);

        if (empty($apply_info)){
            return $this->response($this->error('','加盟商不存在'));
        }

        $apply_model = new MrnaApply();

        $apply_data = [
            'name'=>input('name',''),
            'mobile'=>input('mobile',''),
            'front_pic_id_card'=>input('front_pic_id_card',''),//身份证正面图片
            'reverse_pic_id_card'=>input('reverse_pic_id_card',''),//身份证反面图片
            'id_card_name'=>input('id_card_name',''),//证件姓名
            'id_card_number'=>input('id_card_number',''),//证件号码
            'id_card_type'=>input('id_card_type',''),//证件类型
            'id_card_start_time'=>input('id_card_start_time',''),//证件有效期开始时间
            'id_card_end_time'=>input('id_card_end_time',''),//证件有效期结束时间
            'account_holder'=>input('account_holder',''),//开户人
            'bank_number'=>input('bank_number',''),//银行卡号
            'bank_type'=>input('bank_type',''),//银行卡类型
            'bank_name'=>input('bank_name',''),//开户行名称
            'bank_img'=>input('bank_img',''),
            'id_card_time_type'=>input('id_card_time_type',0)
        ];

        $res = $apply_model->editMrnaApply($apply_data,[['apply_id','=',$apply_info['apply_id']]]);

        return $this->response($res);
    }

    //申请信息
    public function applyInfo()
    {
        $token = $this->checkToken();
        if ($token[ 'code' ] < 0) return $this->response($token);

        $apply_model = new MrnaApply();

        $res = $apply_model->getMrnaApplyInfo([['member_id','=',$this->member_id]]);

        return $this->response($res);
    }


    //权益介绍
    public function introduce()
    {
        $config_model = new ConfigModel();
        $document_info = $config_model->getMemberMrnaDocument(0, 'admin');
        $value = $document_info['data'];
        return $this->response($this->success($value));
    }

    //推广说明
    public function recommend()
    {
        $config_model = new ConfigModel();
        $document_info = $config_model->getMrnaRecommendDocument(0, 'admin');
        $value = $document_info['data'];
        return $this->response($this->success($value));
    }


    //线下支付
    public function offlinepay()
    {
        $out_trade_no = isset($this->params[ 'out_trade_no' ]) ? $this->params[ 'out_trade_no' ] : '';
        $apply_model = new MrnaApply();
        $result  = $apply_model->orderOfflinePay($out_trade_no);
        return $this->response($result);
    }

    //申请退出
    public function applyQuit()
    {
        $token = $this->checkToken();
        if ($token[ 'code' ] < 0) return $this->response($token);

        $apply_id = isset($this->params[ 'apply_id' ]) ? $this->params[ 'apply_id' ] : '';

        $apply_model = new MrnaApply();

        $res = $apply_model->quitMrna($apply_id,$this->member_id);

        return $this->response($res);

    }


    //权益卡销售列表
    public function salelist()
    {
        $token = $this->checkToken();
        if ($token[ 'code' ] < 0) return $this->response($token);

        $order_common_model = new OrderCommonModel();

        $condition = array (
            [ "mrna_member_id", "=", $this->member_id ],
            ["order_type",'=',3],
            ['is_mrna','=',1],
            ['pay_status','=',1]
        );

        $field = 'order_id,create_time,member_id';

        $page_index = isset($this->params[ 'page' ]) ? $this->params[ 'page' ] : 1;
        $page_size = isset($this->params[ 'page_size' ]) ? $this->params[ 'page_size' ] : PAGE_LIST_ROWS;
        $res = $order_common_model->getMrnaOrderPageList($condition, $page_index, $page_size, "create_time desc",$field);
        return $this->response($res);
    }
}